public class Sort {
    public static void insertSort(int[] array){
        // write code  here
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = tmp;
        }

    }


        // 希尔排序
        public static void shellSort(int[] array){
        // write code  here
            int gap = array.length - 1;
            while(gap > 0){
                gap /= 2;
                shell(array,gap);
            }
    }
    private static void swap(int[] array,int i,int j){
        int tmp = array[j];
        array[j] = array[i];
        array[i] = tmp;
    }

    private static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i - gap;
            int tmp = array[i];
            for (; j >= 0; j -= gap) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public static void selectSort(int[] array){
        // write code  here
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }
    public static void creatHeap(int[] array){
        int usedSiez = array.length;
        for(int parent = (usedSiez-1-1)/2; parent >= 0; parent--){
            shiftDown(array,parent,usedSiez);
        }
    }

    private static void shiftDown(int[] array, int parent, int len) {
        int child = parent*2 + 1;
        if(child + 1 < len && array[child] < array[child+1]){
            child++;
        }
        while(len > child){
            if(array[parent] < array[child]){
                swap(array,parent,child);
                parent = child;
                child = 2*parent + 1;
            } else {
                break;
            }
        }
    }

    // 堆排序
    public static void heapSort(int[] array){
        // write code  here
        creatHeap(array);
        int usedSize = array.length - 1;
        for(int i = usedSize; i >= 0; i--){
            swap(array,0,usedSize);
            shiftDown(array,0,usedSize);
            usedSize--;
        }
    }
    public static void bubbleSort(int[] array){
        // write code  here
        for(int i = 0; i < array.length; i++){
            for(int j = i + 1; j < array.length - 1 - i; j++){
                if(array[j] > array[j+1]){
                    swap(array,j,j+1);
                }
            }
        }
    }

// 快速
    public static void quickSort(int[] array){
        // write code  here
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int begin, int end) {
        if(begin >= end){
            return;
        }
        int pivot = func(array,begin,end);
        quick(array,begin,pivot - 1);
        quick(array,pivot+1,end);

    }

    private static int func(int[] array,int left,int right) {
        int tmp = array[left];
        int i = left;
        while(left < right){
            while(left < right && array[right] >= tmp){
                right--;
            }
            while(left < right && array[left] <= tmp){
                left++;
            }
            swap(array,right,left);
        }
        swap(array,left,i);
        return left;
    }
    public static void mergeSort(int[] array){
        // write code  here
        merege(array,0,array.length-1);
    }

    private static void merege(int[] array, int begin, int end) {
        if(begin >= end){
            return;
        }
        int mid = (begin + end) / 2;
        merege(array,begin,mid);
        merege(array,mid+1, end);

        meregeFun(array,begin,mid,end);
    }

    private static void meregeFun(int[] array, int left, int mid, int right) {
        int b1 = left;
        int b2 = mid + 1;
        int e1 = mid;
        int e2 = right;
        int[] tmp = new int[right - left + 1];
        int index = 0;
        while(b1 <= e1 && b2 <= e2){
            if(array[b1] < array[b2]){
                tmp[index++] = array[b1++];
            } else {
                tmp[index++] = array[b2++];
            }
        }
        while(b1 <= e1){
            tmp[index++] = array[b1++];
        }
        while(b2 <= e2){
            tmp[index++] = array[b2++];
        }
        for(int i = 0; i < tmp.length; i++){
            array[i+left] = tmp[i];
        }
    }

    // 归并排序---非递归
    public static void mergeSort1(int[] array){
        // write code  here
        int gap = 1;
        while(gap < array.length){
            for(int i = 0; i < array.length ; i = i + gap*2 ){
                int left = i;
                int mid = left + gap -1;
                int right = mid + gap;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                if(right >= array.length) {
                    right = array.length-1;
                }
                meregeFun(array,left,mid,right);
            }
            gap *= 2;
        }
    }

// 计数排序
    public static void countSort(int[] array){
        // write code  here
        int minval = array[0];
        int maxval = array[0];
        for(int i = 1; i < array.length; i++){
            if(minval > array[i]){
                minval = array[i];
            }
            if(maxval < array[i]){
                maxval = array[i];
            }
        }
        int len = maxval - minval + 1;
        int[] count = new int[len];
        for (int i = 0; i < array.length; i++) {
            count[array[i] - minval]++;
        }
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while(count[i] > 0){
                array[index] = i + minval;
                index++;
                count[i]--;
            }
        }
    }

}
